package cn.nexd.map.rendering.core.helper;

import android.graphics.Matrix;
import android.graphics.RectF;
import android.view.MotionEvent;

import java.util.ArrayList;
import java.util.List;

import cn.nexd.map.rendering.core.componet.data.Node;


public class RectHelper {
    /**
     * 判断点击点是不是在矩形框内
     *
     * @param event
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param matrix
     * @return
     */
    public static boolean withRect(MotionEvent event, float left, float top, float right, float bottom, Matrix matrix) {
        float relP[] = {event.getX(), event.getY()};
        RectF basic = new RectF(left, top, right, bottom);
        matrix.mapRect(basic);
        if (basic.contains(relP[0], relP[1])) {
            return true;
        }
        return false;
    }

    public static boolean withRect(MotionEvent event, float x, float y, int dis) {
        if (event.getX() >= x - dis && event.getX() <= x + dis && event.getY() >= y - dis && event.getY() <= y + dis) {
            return true;
        }
        return false;
    }

    /**
     * 判断两个矩形框是否有重叠
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @param matrix
     * @return
     */
    public static boolean withRect(float left, float top, float right, float bottom, int x1, int y1, int x2, int y2, Matrix matrix) {
        RectF screen = new RectF(x1, y1, x2, y2);
        RectF basic = new RectF(left, top, right, bottom);
        matrix.mapRect(basic);
        if (screen.contains(basic)) {
            return true;
        }
        return false;
    }

    public static boolean insideJudger(List<Node> nodes, float x, float y, float offset_x, float offset_y) {
        if (nodes == null) {
            return false;
        }
        List<Double> angle = new ArrayList<Double>();
        double threshold = 1e-2 * Math.PI * 2;
        for (Node v : nodes) {
            double dx = v.getX() - x;
            double dy = v.getY() - y;
            double ta = 0;
            if (dx == 0) {
                if (dy == 0) {
                    return true;
                } else if (dy < 0) {
                    ta = (-3 / 2) * Math.PI;
                } else {
                    ta = (1 / 2) * Math.PI;
                }
            } else if (dx > 0) {
                ta = Math.atan(dy / dx) + ((dy < 0) ? Math.PI * 2 : 0);
//                System.out.println("dx>0");
            } else {
                ta = Math.atan(dy / dx) + Math.PI;
//                System.out.println("dx<0");
            }
            angle.add(ta);
//            System.out.println("Ang:" + ta);
        }
        double sum_ang = 0;
        for (int i = 0; i < angle.size() - 1; i++) {
            double diff = angle.get(i + 1) - angle.get(i);
            sum_ang += (Math.abs(diff) < Math.PI) ? diff : ((diff < 0) ? diff + Math.PI * 2 : diff - Math.PI * 2);
        }
//        System.out.println(sum_ang);
        if (Math.abs(Math.abs(sum_ang) - Math.PI * 2) < threshold) {
            return true;
        } else {
            return false;
        }
    }
}
